/**
 * 数据源管理相关的TypeScript类型定义
 * 与后端Pydantic模式保持一致
 */

// 数据库类型枚举
export enum DatabaseType {
  MYSQL = 'mysql',
  POSTGRESQL = 'postgresql',
  ORACLE = 'oracle',
  SQLSERVER = 'sqlserver',
  DORIS = 'doris',
  KAFKA = 'kafka'
}

// 测试状态枚举
export enum TestStatus {
  SUCCESS = 'success',
  FAILED = 'failed',
  UNKNOWN = 'unknown'
}

// 数据库类型显示名称映射
export const DatabaseTypeLabels: Record<DatabaseType, string> = {
  [DatabaseType.MYSQL]: 'MySQL',
  [DatabaseType.POSTGRESQL]: 'PostgreSQL',
  [DatabaseType.ORACLE]: 'Oracle',
  [DatabaseType.SQLSERVER]: 'SQL Server',
  [DatabaseType.DORIS]: 'Apache Doris',
  [DatabaseType.KAFKA]: 'Apache Kafka'
};

// 数据库默认端口映射
export const DatabaseDefaultPorts: Record<DatabaseType, number> = {
  [DatabaseType.MYSQL]: 3306,
  [DatabaseType.POSTGRESQL]: 5432,
  [DatabaseType.ORACLE]: 1521,
  [DatabaseType.SQLSERVER]: 1433,
  [DatabaseType.DORIS]: 9030, // MySQL协议端口，用于查询
  [DatabaseType.KAFKA]: 9092
};

// Doris HTTP端口映射（用于Stream Load等HTTP API）
export const DorisHttpPorts: Record<number, number> = {
  9030: 18030, // 用户环境的端口映射
  9031: 18031,
  9032: 18032
};

// 测试状态显示名称映射
export const TestStatusLabels: Record<TestStatus, string> = {
  [TestStatus.SUCCESS]: '成功',
  [TestStatus.FAILED]: '失败',
  [TestStatus.UNKNOWN]: '未知'
};

// 测试状态颜色映射
export const TestStatusColors: Record<TestStatus, string> = {
  [TestStatus.SUCCESS]: 'success',
  [TestStatus.FAILED]: 'error',
  [TestStatus.UNKNOWN]: 'default'
};

// 数据源基础接口
export interface DataSourceBase {
  name: string;
  display_name: string;
  description?: string;
  type: DatabaseType;
  host: string;
  port: number;
  database_name: string;
  username: string;
  connection_params?: Record<string, any>;
  is_active: boolean;
  connection_timeout: number;
  max_connections: number;
}

// 创建数据源请求接口
export interface DataSourceCreate extends DataSourceBase {
  password: string;
}

// 更新数据源请求接口
export interface DataSourceUpdate {
  display_name?: string;
  description?: string;
  host?: string;
  port?: number;
  database_name?: string;
  username?: string;
  password?: string;
  connection_params?: Record<string, any>;
  is_active?: boolean;
  connection_timeout?: number;
  max_connections?: number;
}

// 数据源响应接口
export interface DataSource extends DataSourceBase {
  id: number;
  last_test_at?: string;
  last_test_status: TestStatus;
  last_test_message?: string;
  connection_string_masked: string;
  is_healthy: boolean;
  created_by?: number;
  updated_by?: number;
  created_at: string;
  updated_at: string;
}

// 数据源列表响应接口
export interface DataSourceListResponse {
  datasources: DataSource[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

// 连接测试请求接口
export interface ConnectionTestRequest {
  type: DatabaseType;
  host: string;
  port: number;
  database_name: string;
  username: string;
  password: string;
  connection_params?: Record<string, any>;
  connection_timeout: number;
}

// 连接测试响应接口
export interface ConnectionTestResponse {
  success: boolean;
  message: string;
  response_time?: number;
  error_details?: Record<string, any>;
  database_info?: Record<string, any>;
}

// 测试日志接口
export interface TestLog {
  id: number;
  test_status: TestStatus;
  test_message?: string;
  response_time?: number;
  error_details?: Record<string, any>;
  tested_by?: number;
  tested_at: string;
}

// 批量测试请求接口
export interface BatchTestRequest {
  datasource_ids: number[];
}

// 批量测试响应接口
export interface BatchTestResponse {
  results: Array<{
    datasource_id: number;
    datasource_name: string;
    success: boolean;
    message: string;
    response_time?: number;
  }>;
  success_count: number;
  failed_count: number;
  total_count: number;
}

// 批量操作请求接口
export interface BatchOperationRequest {
  datasource_ids: number[];
  operation: 'delete' | 'enable' | 'disable';
}

// 数据源统计接口
export interface DataSourceStats {
  total_count: number;
  active_count: number;
  inactive_count: number;
  healthy_count: number;
  unhealthy_count: number;
  type_distribution: Record<string, number>;
  recent_test_results: Array<{
    datasource_name: string;
    test_status: TestStatus;
    test_message?: string;
    response_time?: number;
    tested_at: string;
  }>;
}

// 数据源导入请求接口
export interface DataSourceImportRequest {
  datasources: DataSourceCreate[];
  overwrite_existing: boolean;
}

// 数据源导入响应接口
export interface DataSourceImportResponse {
  success_count: number;
  failed_count: number;
  skipped_count: number;
  total_count: number;
  errors: Array<Record<string, any>>;
}

// 数据源导出响应接口
export interface DataSourceExportResponse {
  datasources: Array<Record<string, any>>;
  export_time: string;
  total_count: number;
}

// 表格查询参数接口
export interface DataSourceQueryParams {
  page?: number;
  page_size?: number;
  search?: string;
  db_type?: DatabaseType;
  is_active?: boolean;
  test_status?: TestStatus;
}

// 表单验证规则接口
export interface DataSourceFormRules {
  name: any[];
  display_name: any[];
  type: any[];
  host: any[];
  port: any[];
  database_name: any[];
  username: any[];
  password: any[];
  connection_timeout: any[];
  max_connections: any[];
}

// 表格列配置接口
export interface DataSourceTableColumn {
  title: string;
  dataIndex: string;
  key: string;
  width?: number;
  fixed?: 'left' | 'right';
  sorter?: boolean;
  filters?: Array<{ text: string; value: any }>;
  render?: (value: any, record: DataSource, index: number) => React.ReactNode;
}

// 操作按钮配置接口
export interface ActionButtonConfig {
  key: string;
  label: string;
  icon: React.ReactNode;
  permission: string;
  danger?: boolean;
  disabled?: (record: DataSource) => boolean;
  onClick: (record: DataSource) => void;
}

// 批量操作配置接口
export interface BatchActionConfig {
  key: string;
  label: string;
  icon: React.ReactNode;
  permission: string;
  danger?: boolean;
  confirmTitle?: string;
  confirmContent?: string;
  onClick: (selectedIds: number[]) => void;
}

// 搜索筛选配置接口
export interface SearchFilterConfig {
  searchPlaceholder: string;
  typeFilterOptions: Array<{ label: string; value: DatabaseType }>;
  statusFilterOptions: Array<{ label: string; value: boolean }>;
  testStatusFilterOptions: Array<{ label: string; value: TestStatus }>;
}

// 分页配置接口
export interface PaginationConfig {
  current: number;
  pageSize: number;
  total: number;
  showSizeChanger: boolean;
  showQuickJumper: boolean;
  showTotal: (total: number, range: [number, number]) => string;
}

// 组件状态接口
export interface DataSourceManagementState {
  loading: boolean;
  datasources: DataSource[];
  selectedRowKeys: number[];
  searchText: string;
  selectedType?: DatabaseType;
  selectedStatus?: boolean;
  selectedTestStatus?: TestStatus;
  pagination: PaginationConfig;
  isModalVisible: boolean;
  isTestModalVisible: boolean;
  editingDataSource?: DataSource;
  testingDataSource?: DataSource;
}

// 权限配置接口
export interface DataSourcePermissions {
  canView: boolean;
  canCreate: boolean;
  canUpdate: boolean;
  canDelete: boolean;
  canTest: boolean;
  canImport: boolean;
  canExport: boolean;
  canManage: boolean;
}

// API响应基础接口
export interface ApiResponse<T = any> {
  data: T;
  message: string;
  success: boolean;
}

// 错误响应接口
export interface ApiError {
  message: string;
  detail?: string;
  status?: number;
}

// 表单字段配置接口
export interface FormFieldConfig {
  name: string;
  label: string;
  type: 'input' | 'select' | 'number' | 'password' | 'textarea' | 'switch';
  required?: boolean;
  placeholder?: string;
  options?: Array<{ label: string; value: any }>;
  rules?: any[];
  dependencies?: string[];
  tooltip?: string;
}

// 连接测试结果接口
export interface TestResult {
  success: boolean;
  message: string;
  response_time?: number;
  database_info?: {
    database_type: string;
    version: string;
    current_database: string;
    [key: string]: any;
  };
  error_details?: {
    error_type: string;
    error_message: string;
    database_type: string;
    host: string;
    port: number;
  };
}
